<?php
namespace Admin\Controller;
use Think\Controller;
class PlateformController extends BaseController{
    public function index(){
        $m=M('Plateform');
		$listrows=20;
		$page=I('p')?I('p'):1;
		$plateform_name=I('plateform_name');
		$plateform_code=I('plateform_code');
		if(!empty($plateform_name)){
			$map['plateform_name']=I('plateform_name');
		}
		if(!empty($plateform_code)){
			$map['plateform_code']=I('plateform_code');
		}
		
		$count=$m->where($map)->count();
		$list=$m->where($map)->order('id desc')->page("$page,$listrows")->select();
		// echo $m->getlastsql();
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		
		$this->assign('list',$list);
		$this->assign('count',$count);
		$this->assign('page',$show);
        $this->display();
    }
	public function add_plateform(){
		if(IS_POST){
			$param=I('param.','','trim');

			$m=M('Plateform');
			$map1['plateform_code']=$param['plateform_code'];
			$map2['plateform_name']=$param['plateform_name'];
			
			$m1_info=$m->where($map1)->find();
			$m2_info=$m->where($map2)->find();
			
			if(!empty($m1_info)){
				alert_json(0,'此平台编号已经存在');
			}else if(!empty($m2_info)){
				alert_json(0,'此平台名称已经存在');
			}else{
				
				$data['plateform_code']=$param['plateform_code'];
				$data['plateform_name']=$param['plateform_name'];
				$data['add_time']=time();
				// dump($m);
				$res=$m->add($data);
				// echo $m->_sql();
				if(!$res){
					alert_json(0,'出错');
				}else{
					alert_json(1,'成功');
				}
			}
		}else{
			$this->display();
		}
	}
	public function edit_plateform(){
		if(IS_POST){
			$param=I('param.','','trim');

			$m=M('Plateform');
			$map['id']=$param['id'];

			$data['plateform_name']=$param['plateform_name'];
			$res=$m->where($map)->save($data);
			if($res===false){
				alert_json(0,'出错');
			}else{
				alert_json(1,'成功');
			}

		}else{
			$id=I('id');
			$m=M('Plateform');
			$detail=$m->where(array('id'=>$id))->find();
			$this->assign('detail',$detail);
			$this->display();
		}
	}
	public function del_plateform(){
		$id=I('id');
		$m=M('Plateform');
		$res=$m->where(array('id'=>$id))->delete();
		if(!$res){
			echo "操作失败";
		}else{
			echo "y";
		}
	}
	public function declare_price(){
        $m=M('UserPlateform');
		$u=M('User');
		$mp=M('Plateform');
		$listrows=20;
		$page=I('p')?I('p'):1;
		$company=I('company');
		$plateform_code=I('plateform_code');
		if(!empty($company)){
			$map['u.company']=array('like',"%$company%");
		}
		if(!empty($plateform_code)){
			$map['m.plateform_code']=I('plateform_code');
		}
		
		$count=$m->alias('m')->join("__USER__ u on m.user_id=u.id",'left')->where($map)->count();
		$list=$m->alias('m')->field('m.id,m.cate,m.user_id,m.plateform_code,m.add_time,u.company')->join("__USER__ u on m.user_id=u.id",'left')->where($map)->order('m.id desc')->page("$page,$listrows")->select();
		foreach($list as $k=>$v){
			$list[$k]['plateform_name']=$mp->where(array('plateform_code'=>$v['plateform_code']))->getField('plateform_name');
			if($v['cate']==1){
				$list[$k]['cate_name']='申报价';
			}
			if($v['cate']==2){
				$list[$k]['cate_name']='销售价';
			}
		}
		// echo $m->getlastsql();
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		
		
		$map_u['status']=1;
		$u_list=$u->where($map_u)->select();
		$m_list=$mp->select();
		
		$this->assign('m_list',$m_list);
		$this->assign('u_list',$u_list);
			
		$this->assign('list',$list);
		$this->assign('count',$count);
		$this->assign('page',$show);
        $this->display();
    }
	public function add_declare_price(){
		if(IS_POST){
			$param=I('param.','','trim');
			$data['plateform_code']=$param['plateform_code'];
			$data['user_id']=$param['user_id'];
			$data['cate']=$param['cate'];
			$data['add_time']=time();
			$m=M('UserPlateform');
			$map['user_id']=$param['user_id'];
			$map['plateform_code']=$param['plateform_code'];
			$m_info=$m->where($map)->find();
			if(empty($m_info)){
				$res=$m->add($data);
				if(!$res){
					alert_json(0,'出错');
				}else{
					alert_json(1,'成功');
				}
			}else{
				alert_json(0,'此项已经存在');
			}
		}else{
			$u=M('User');
			$m=M('Plateform');
			$map['status']=1;
			$map['owner_status']=1;
			$u_list=$u->where($map)->select();
			$m_list=$m->select();
			
			$this->assign('m_list',$m_list);
			$this->assign('u_list',$u_list);
			$this->display();
		}
	}
	public function edit_declare_price(){
		if(IS_POST){
			$param=I('param.','','trim');

			$m=M('UserPlateform');
			$map['id']=$param['id'];

			$data['cate']=$param['cate'];
			$res=$m->where($map)->save($data);
			if($res===false){
				alert_json(0,'出错');
			}else{
				alert_json(1,'成功');
			}
		}else{
			$id=I('id');
			$m=M('UserPlateform');
			$detail=$m->where(array('id'=>$id))->find();
			
			$u=M('User');
			$m=M('Plateform');
			$map['status']=1;
			$map['owner_status']=1;
			$u_list=$u->where($map)->select();
			$m_list=$m->select();
			
			$this->assign('m_list',$m_list);
			$this->assign('u_list',$u_list);
			
			$this->assign('detail',$detail);
			$this->display();
		}
	}
	public function del_declare_price(){
		$id=I('id');
		$m=M('UserPlateform');
		$res=$m->where(array('id'=>$id))->delete();
		if(!$res){
			echo "操作失败";
		}else{
			echo "y";
		}
	}
}